package com.autotest.nore.common.utils;


import com.autotest.nore.mapper.TestFileMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BodyParserUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private static TestFileMapper testFileMapper;

    public static Map<String, Object> parseBody(String processedBody) {
        Map<String, Object> requestBody = new HashMap<>();

        try {
            JsonNode rootNode = objectMapper.readTree(processedBody);
            String type = rootNode.get("type").asText();

            switch (type) {
                case "json":
                    String json = rootNode.get("json").asText();
                    requestBody.put("body", json);
                    requestBody.put("contentType", "application/json");
                    break;
                case "form-urlencoded":
                    JsonNode formNode = rootNode.get("form");
                    if (formNode.isArray()) {
                        Map<String, Object> formData = new HashMap<>();
                        for (JsonNode formElement : formNode) {
                            String name = formElement.get("name").asText();
                            String value = formElement.get("value").asText();
                            String dataType = formElement.get("type").asText();
                            switch (dataType){
                                case "File":
                                    //处理文件类型
                                    File file = new File(testFileMapper.getTestFile(value).getFilePath());
                                    formData.put(name, file);
                                    break;
                                case "String":
                                    // 处理字符串类型
                                    formData.put(name, value);
                                    break;
                                case "Int":
                                    // 处理整数类型
                                    formData.put(name, Integer.parseInt(value));
                                    break;
                                case "Float":
                                    // 处理浮点数类型
                                    formData.put(name, Float.parseFloat(value));
                                    break;
                                case "Boolean":
                                    // 处理布尔类型
                                    formData.put(name, Boolean.parseBoolean(value));
                                    break;
                                case "JSONObject":
                                    // 处理 JSON 对象类型
                                    formData.put(name, objectMapper.readValue(value, Map.class));
                                    break;
                                case "JSONArray":
                                    // 处理 JSON 数组类型
                                    formData.put(name, objectMapper.readValue(value, List.class));
                                    break;
                                default:
                                    continue;
                            }
                        }
                        requestBody.put("body", formData);
                        requestBody.put("contentType", "application/x-www-form-urlencoded");
                    }
                    break;
                case "form-data":
                    JsonNode formDataNode = rootNode.get("form");
                    if (formDataNode.isArray()) {
                        Map<String, Object> formData = new HashMap<>();
                        for (JsonNode formElement : formDataNode) {
                            String name = formElement.get("name").asText();
                            String value = formElement.get("value").asText();
                            String dataType = formElement.get("type").asText();
                            switch (dataType){
                                case "File":
                                    //处理文件类型
                                    File file = new File(testFileMapper.getTestFile(value).getFilePath());
                                    formData.put(name, file);
                                    break;
                                case "String":
                                    // 处理字符串类型
                                    formData.put(name, value);
                                    break;
                                case "Int":
                                    // 处理整数类型
                                    formData.put(name, Integer.parseInt(value));
                                    break;
                                case "Float":
                                    // 处理浮点数类型
                                    formData.put(name, Float.parseFloat(value));
                                    break;
                                case "Boolean":
                                    // 处理布尔类型
                                    formData.put(name, Boolean.parseBoolean(value));
                                    break;
                                case "JSONObject":
                                    // 处理 JSON 对象类型
                                    formData.put(name, objectMapper.readValue(value, Map.class));
                                    break;
                                case "JSONArray":
                                    // 处理 JSON 数组类型
                                    formData.put(name, objectMapper.readValue(value, List.class));
                                    break;
                                default:
                                    continue;
                            }
                        }
                        requestBody.put("body", formData);
                        requestBody.put("contentType", "multipart/form-data");
                    }
                    break;
                case "text":
                    String rawText = rootNode.get("raw").asText();
                    requestBody.put("body", rawText);
                    requestBody.put("contentType", "text");
                    break;
                case "html":
                    String rawHtml = rootNode.get("raw").asText();
                    requestBody.put("body", rawHtml);
                    requestBody.put("contentType", "html");
                    break;
                case "xml":
                    String rawXml = rootNode.get("raw").asText();
                    requestBody.put("body", rawXml);
                    requestBody.put("contentType", "xml");
                    break;
                case "none":
                    requestBody.put("body", "");
                    requestBody.put("contentType", "none");
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported request type: " + type);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return requestBody;
    }
}
